package com.service.infoEntry.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.infoEntry.dto.CalendarDto;
import com.service.infoEntry.dto.UserDto;
import com.service.infoEntry.service.CalendarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.service.infoEntry.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2021/11/10 16:54
 * @description：日历接口
 */
@RestController
@CrossOrigin
@Api("日历接口")
@RequestMapping("/calendar")
public class CalendarController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CalendarService calendarService;

    @ApiOperation(value = "添加日历事件")
    @RequestMapping(value = "/addEvent",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result addEvent(@RequestBody CalendarDto calendarDto,
                                 @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                calendarDto.setUserId(userId);
                calendarService.addEvent(calendarDto);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "修改事件")
    @RequestMapping(value = "/updateEvent",method = RequestMethod.POST)
    @ResponseBody
    public Result updateEvent(@RequestBody CalendarDto calendarDto,
                                      @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                if(calendarDto.getUserId().equals(userId))
                {
                    String value = redisUtil.getValue(cloudAuthToken + userId);
                    UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                    return calendarService.updateEvent(calendarDto);
                }else {
                    return Result.failure(USER_NO_AUTHORITY,INSUFFICIENT_USER_PERMISSIONS);
                }
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "删除事件")
    @RequestMapping(value = "/deleteEvent",method = RequestMethod.DELETE)
    @ResponseBody
    public Result deleteEvent(@RequestParam("eventId")@ApiParam("事件id") String eventId,
                                  @RequestHeader(value = "cloud-Auth-Token",required = false) @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                return calendarService.deleteEvent(eventId,userId);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @RequestMapping(value =  "queryEventByDate",method = RequestMethod.GET,produces="application/json")
    @ApiOperation(value = "通过日期查询事件")
    @ResponseBody
    public Result queryEventByDate(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam("teamId") String teamId,
                                   @RequestParam("date") Date date) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<Map<String,Object>> data = calendarService.queryEventByDate(teamId,date,userId);
                return Result.ok().setData(data);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
